package com.auto.autoapp.data.repository

import com.auto.autoapp.data.api.UserApi
import com.auto.autoapp.data.model.PageData
import com.auto.autoapp.data.model.User
import com.auto.autoapp.network.ApiException
import com.auto.autoapp.network.BaseRepository
import com.auto.autoapp.network.NetworkResult
import com.auto.autoapp.network.RetrofitClient
import com.auto.autoapp.network.create
import kotlinx.coroutines.flow.Flow

/**
 * 用户数据仓库
 * 负责处理用户相关的数据请求
 */
class UserRepository : BaseRepository() {
    
    private val userApi: UserApi = RetrofitClient.create()
    /**
     * 获取用户列表
     */
    fun getUserList(page: Int = 1, pageSize: Int = 20): Flow<NetworkResult<PageData<User>>> {
        return executeRequestWithTransform(
            apiCall = { userApi.getUserList(page, pageSize) },
            transform = { response ->
                if (response.isSuccess() && response.data != null) {
                    response.data
                } else {
                    throw ApiException.BusinessException(
                        response.message?:"",
                        response.code
                    )
                }
            }
        )
    }
    
    /**
     * 获取用户详情
     */
    fun getUserDetail(userId: Int): Flow<NetworkResult<User>> {
        return executeRequestWithTransform(
            apiCall = { userApi.getUserDetail(userId) },
            transform = { response ->
                if (response.isSuccess() && response.data != null) {
                    response.data
                } else {
                    throw ApiException.BusinessException(
                        response.message?:"",
                        response.code
                    )
                }
            }
        )
    }
    
    /**
     * 创建用户
     */
    fun createUser(name: String, email: String, phone: String? = null): Flow<NetworkResult<User>> {
        return executeRequestWithTransform(
            apiCall = { userApi.createUser(name, email, phone) },
            transform = { response ->
                if (response.isSuccess() && response.data != null) {
                    response.data
                } else {
                    throw ApiException.BusinessException(
                        response.message?:"",
                        response.code
                    )
                }
            }
        )
    }
    
    /**
     * 更新用户信息
     */
    fun updateUser(userId: Int, user: User): Flow<NetworkResult<User>> {
        return executeRequestWithTransform(
            apiCall = { userApi.updateUser(userId, user) },
            transform = { response ->
                if (response.isSuccess() && response.data != null) {
                    response.data
                } else {
                    throw ApiException.BusinessException(
                        response.message?:"",
                        response.code
                    )
                }
            }
        )
    }
    
    /**
     * 删除用户
     */
    fun deleteUser(userId: Int): Flow<NetworkResult<Boolean>> {
        return executeRequestWithTransform(
            apiCall = { userApi.deleteUser(userId) },
            transform = { response ->
                if (response.isSuccess()) {
                    true
                } else {
                    throw ApiException.BusinessException(
                        response.message?:"",
                        response.code
                    )
                }
            }
        )
    }
}


